﻿/*----------------------------------------------------------------
// Copyright (C) 2013 上海汽车集团股份有限公司
// 版权所有。
//
// 文件名：menu.js
// 文件功能描述：菜单处理和页面布局处理
//              文件依赖： 
//                       jquery-1.8.0.min.js、
//                       jquery.ztree.all-3.5.min.js 
//
//
// 创建标识：  曹珊 2013/8/16
//
// 修改标识：
// 修改描述：
//
//----------------------------------------------------------------*/ 

//菜单树
var zTree, rMenu, zNodes;
$(function () {
    var setting = {
        view: {
            dblClickExpand: true,
            showLine: true,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            onRightClick: OnRightClick,
            onExpand: zTreeOnExpand,
            onCollapse: zTreeOnExpand
        }
    };

    function zTreeOnExpand(event, treeId, treeNode) {
        if (treeNode) {
            var json = { menu_ids: getTreeStatus() };
            $.pubAjax({
                errorHandle: false, //服务器错误不处理
                url: "/Server/Common.aspx?Action=SaveMenuStatus",
                data: json,
                sync: true,
                success: function () { ; }
            });
        }
    };

    $(document).ready(function () {
        $.fn.zTree.init($("#tree"), setting, zNodes);

        zTree = $.fn.zTree.getZTreeObj("tree");
        rMenu = $("#treeRightMenu");

        rMenu.bind('click', function () {
            hideRMenu();
        });
    });

    $(".userinfo").html("欢迎您！" + userName.Decode() + ". " + $.SaicConvert(new Date(), 'yyyy-MM-dd'));
});

function OnRightClick(event, treeId, treeNode) {
    if (treeNode && !treeNode.isParent) {
        zTree.selectNode(treeNode);
        showRMenu("node", event.clientX, event.clientY);

        var html = "<a href=\"" + treeNode.url + "\" target=\"_blank\">在新标签页中打开</a>";
        $("#m_add").html(html);
    }
}
function getTreeStatus() {
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var node = treeObj.getNodesByParam("isParent", true, null);
    var ids = [];
    for (var i = 0; i < node.length; i++) {
        if (node[i].open === true) {
            ids.push(node[i].id);
        }
    }
    return ids.toString();
}
function showRMenu(type, x, y) {
    $(".treeContent").scrollLeft(0);
    rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
    $("body").bind("mousedown", onBodyMouseDown);
}
function hideRMenu() {
    if (rMenu) rMenu.css({ "visibility": "hidden" });
    $("body").unbind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event) {
    if (!(event.target.id == "treeRightMenu" || $(event.target).parents("#treeRightMenu").length > 0)) {
        rMenu.css({ "visibility": "hidden" });
    }
}
function CollapseTree() {
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    treeObj.expandAll(false);
}

//***********Layout**************/
var minWdith = 1024;
function Layout() {
    var height = $(window).height() - 5;
    var width = $(window).width() - 5;
    width = width < minWdith ? minWdith : width;

    $("#main").height(height).width(width);

    var contentHeight = height - $("#head").outerHeight() - 10;

    var treeHeight = height - $(".left_top").outerHeight() - $("#head").outerHeight();

    var rightWidth = width - $("#left").outerWidth() - 20;
    $("#left").height(contentHeight);
    $(".treeContent").height(treeHeight);
    $("#rightmian").height(contentHeight).width(rightWidth);

    //报表页面
    if ($('.reportDIV').length > 0) {
        $('.reportDIV').height(GetGridContainerHeight() + 80).width(rightWidth);
        $('.reportContainer').height($('.reportDIV').height() - $('.reportTitle').height() - 2).width($('.reportTitle').width() - 2);
    }
}

function GetGridContainerHeight() {
    return $(window).height() - $(".Search_tab").height() - 130 - $("#head").outerHeight() - 10;
}

function GetGridContainerWidth() {
    var width = $(window).width();
    var w = width > minWdith ? 28 : 23;
    width = width < minWdith ? (minWdith + 5) : width;
    width = width - $("#left").width() - w;
    return width;
}

$(function () {
    Layout();
    $(window).resize(function () {
        Layout();
    });

    $("#button4").click();
})

function showLeft(o) {
    if ($(o).hasClass("aleft")) {
        $("#adownID").hide();
        $(".treeContent").hide();
        $("#left").width(30);
        $(o).addClass('aright').removeClass("aleft");
    } else {
        $("#adownID").show();
        $(".treeContent").show();
        $("#left").width(167);
        $(o).addClass('aleft').removeClass('aright');
    }

    $(window).resize();
}

